<?php

namespace app\common\model;

use basic\ModelBasic;

class UserLevel extends ModelBasic
{
	use \traits\ModelTrait;

	//1永久 2：指定日期
	public static $expire_type = [
		1    =>    '永久有效',
		2    =>    '指定日期',
	];
	
	// 等级类型 1:大众会员 2:黄金会员 3:铂金会员 4:钻石会员
	public static $grade_type = [
		1    =>    '黄金会员',
		2    =>    '铂金会员',
		3    =>    '钻石会员',
		// 4    =>    '钻石会员',
	];

    /**
     * 前台会员操作允许的类型
     * @var [type]
     */
    public static $act_type = [
        'open'  => '开通',
        'raise' => '升级',
        'renew' => '续费',
    ];

	/**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
        $count = self::setWhere($where)->count();
        $data  = [];
        if ( $count == 0 ) {
            return compact('data','count');
        }

        $data=self::setWhere($where,'m')
        ->field("m.*")
        ->page($where['page'] ?? 1,$where['limit'] ?? 60)
        ->select();

        if ( !empty($data) ) {
            $data = $data->toArray();
            $data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * [getOne description]
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
            ->field("m.*")
            ->find();

        if ( empty($item) ) {
            return [];
        }

        $item = $item->toArray();
        return static::process_list([$item])[0];
    }

     /**
     * 处理列表数据
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        foreach ($list as $key => &$value) {
            $value['expire_type_display'] = static::$expire_type[$value['expire_type']] ?? '-';
            $value['grade_type_display'] = static::$grade_type[$value['grade_type']] ?? '-';
            //$value['days'] = date('Y-m-d' , strtotime($value['expire_time']));
        }
        return $list;
    }

	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null)
    {
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( isset($where['order']) && $where['order'] != '' ) {
            $model->order(sprintf('%s.%s' , $alias, $where['order']));
        } else {
            $model->order(sprintf('%s.id ASC' , $alias));
        }

        return $model;
    }

    /**
     * 计算会员等级可退费用
     * @Author   FW
     * @DateTime 2024-04-09
     * @Email    123763712@qq.com
     * @param    [type]           $levelId [description]
     * @param    [type]           $uid     [description]
     * @return   [type]                    [description]
     */
    public static function calcUserLevelRefund($uid)
    {
        $data = [
            'leftDays' => 0,
            'leftAmt'  => 0,
            'expire_at'=> ''
        ];

        // 当前会员等级
        $currenLevelId = UserLevel::alias('ul')->join('__USER__ u','ul.id=u.level_id')->where('u.uid',$uid)->value('id');
        if ( !$currenLevelId ) return $data;

        $record = UserLevelRecords::alias('m')
        ->field('m.expire_date,m.obtain_date,o.order_amt')
        ->join('__ORDER__ o','o.order_outer_id = m.id AND o.uid = m.uid')
        ->where(['m.uid'=>$uid,'m.level_id'=>$currenLevelId])
        ->find();
        if ( empty($record) ) return $data;

        $totalDays = dateDiffDays($record->expire_date , $record->obtain_date);
        if ( !$totalDays ) return $data;

        $dayAvgPrice      = bcdiv($record->order_amt , $totalDays,2);
        $data['leftDays'] = dateDiffDays($record->expire_date , date('Y-m-d'));
        $data['leftAmt']  = bcmul($dayAvgPrice, $data['leftDays'] , 2);
        $data['expire_at']= $record->expire_date;

        return $data;
    }
}//Class End
